/* $Id: list.h 3553 2011-05-05 06:14:19Z nanang $ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#ifndef __DPL_LIST_H__
#define __DPL_LIST_H__

/**
 * @file list.h
 * @brief Linked List data structure.
 */

#include <dpl/types.h>



/*
 * @defgroup DPL_DS Data Structure.
 */

/**
 * @defgroup DPL_LIST Linked List
 * @ingroup DPL_DS
 * @{
 *
 * List in DPLLIB is implemented as doubly-linked list, and it won't require
 * dynamic memory allocation (just as all DPLLIB data structures). The list here
 * should be viewed more like a low level C list instead of high level C++ list
 * (which normally are easier to use but require dynamic memory allocations),
 * therefore all caveats with C list apply here too (such as you can NOT put
 * a node in more than one lists).
 *
 * \section dpl_list_example_sec Examples
 *
 * See below for examples on how to manipulate linked list:
 *  - @ref page_dpllib_samples_list_c
 *  - @ref page_dpllib_list_test
 */


/**
 * Use this macro in the start of the structure declaration to declare that
 * the structure can be used in the linked list operation. This macro simply
 * declares additional member @a prev and @a next to the structure.
 * @hideinitializer
 */
#define DPL_DECL_LIST_MEMBER(type)                       \
                                   /** List @a prev. */ \
                                   type *prev;          \
                                   /** List @a next. */ \
                                   type *next 


/**
 * This structure describes generic list node and list. The owner of this list
 * must initialize the 'value' member to an appropriate value (typically the
 * owner itself).
 */
struct dpl_list
{
    DPL_DECL_LIST_MEMBER(void);
};


/**
 * Initialize the list.
 * Initially, the list will have no member, and function dpl_list_empty() will
 * always return nonzero (which indicates TRUE) for the newly initialized 
 * list.
 *
 * @param node The list head.
 */
static void dpl_list_init(dpl_list_type * node)
{
    ((dpl_list*)node)->next = ((dpl_list*)node)->prev = node;
}


/**
 * Check that the list is empty.
 *
 * @param node	The list head.
 *
 * @return Non-zero if the list is empty, or zero if it is not empty.
 *
 */
static int dpl_list_empty(const dpl_list_type * node)
{
    return ((dpl_list*)node)->next == node;
}


/**
 * Insert the node to the list before the specified element position.
 *
 * @param pos	The element to which the node will be inserted before. 
 * @param node	The element to be inserted.
 *
 * @return void.
 */
static void	dpl_list_insert_before(dpl_list_type *pos, dpl_list_type *node);


/**
 * Insert the node to the back of the list. This is just an alias for
 * #dpl_list_insert_before().
 *
 * @param list	The list. 
 * @param node	The element to be inserted.
 */
static void dpl_list_push_back(dpl_list_type *list, dpl_list_type *node)
{
    dpl_list_insert_before(list, node);
}


/**
 * Inserts all nodes in \a nodes to the target list.
 *
 * @param lst	    The target list.
 * @param nodes	    Nodes list.
 */
static void dpl_list_insert_nodes_before(dpl_list_type *lst,
					   dpl_list_type *nodes);

/**
 * Insert a node to the list after the specified element position.
 *
 * @param pos	    The element in the list which will precede the inserted 
 *		    element.
 * @param node	    The element to be inserted after the position element.
 *
 * @return void.
 */
static void dpl_list_insert_after(dpl_list_type *pos, dpl_list_type *node);


/**
 * Insert the node to the front of the list. This is just an alias for
 * #dpl_list_insert_after().
 *
 * @param list	The list. 
 * @param node	The element to be inserted.
 */
static void dpl_list_push_front(dpl_list_type *list, dpl_list_type *node)
{
    dpl_list_insert_after(list, node);
}


/**
 * Insert all nodes in \a nodes to the target list.
 *
 * @param lst	    The target list.
 * @param nodes	    Nodes list.
 */
static void dpl_list_insert_nodes_after(dpl_list_type *lst,
					  dpl_list_type *nodes);


/**
 * Remove elements from the source list, and insert them to the destination
 * list. The elements of the source list will occupy the
 * front elements of the target list. Note that the node pointed by \a list2
 * itself is not considered as a node, but rather as the list descriptor, so
 * it will not be inserted to the \a list1. The elements to be inserted starts
 * at \a list2->next. If \a list2 is to be included in the operation, use
 * \a dpl_list_insert_nodes_before.
 *
 * @param list1	The destination list.
 * @param list2	The source list.
 *
 * @return void.
 */
static void dpl_list_merge_first(dpl_list_type *list1, dpl_list_type *list2);


/**
 * Remove elements from the second list argument, and insert them to the list 
 * in the first argument. The elements from the second list will be appended
 * to the first list. Note that the node pointed by \a list2
 * itself is not considered as a node, but rather as the list descriptor, so
 * it will not be inserted to the \a list1. The elements to be inserted starts
 * at \a list2->next. If \a list2 is to be included in the operation, use
 * \a dpl_list_insert_nodes_before.
 *
 * @param list1	    The element in the list which will precede the inserted 
 *		    element.
 * @param list2	    The element in the list to be inserted.
 *
 * @return void.
 */
static void dpl_list_merge_last( dpl_list_type *list1, dpl_list_type *list2);


/**
 * Erase the node from the list it currently belongs.
 *
 * @param node	    The element to be erased.
 */
static void dpl_list_erase(dpl_list_type *node);


/**
 * Find node in the list.
 *
 * @param list	    The list head.
 * @param node	    The node element to be searched.
 *
 * @return The node itself if it is found in the list, or NULL if it is not 
 *         found in the list.
 */
static dpl_list_type* dpl_list_find_node(dpl_list_type *list, 
					  dpl_list_type *node);


/**
 * Search the list for the specified value, using the specified comparison
 * function. This function iterates on nodes in the list, started with the
 * first node, and call the user supplied comparison function until the
 * comparison function returns ZERO.
 *
 * @param list	    The list head.
 * @param value	    The user defined value to be passed in the comparison 
 *		    function
 * @param comp	    The comparison function, which should return ZERO to 
 *		    indicate that the searched value is found.
 *
 * @return The first node that matched, or NULL if it is not found.
 */
static dpl_list_type* dpl_list_search(dpl_list_type *list, void *value,
				       int (*comp)(void *value, 
						   const dpl_list_type *node)
				       );


/**
 * Traverse the list to get the number of elements in the list.
 *
 * @param list	    The list head.
 *
 * @return	    Number of elements.
 */
static dpl_size_t dpl_list_size(const dpl_list_type *list);


/**
 * @}
 */




/* Internal */
static void dpl_link_node(dpl_list_type *prev, dpl_list_type *next)
{
    ((dpl_list*)prev)->next = next;
    ((dpl_list*)next)->prev = prev;
}

static void  dpl_list_insert_after(dpl_list_type *pos, dpl_list_type *node)
{
    ((dpl_list*)node)->prev = pos;
    ((dpl_list*)node)->next = ((dpl_list*)pos)->next;
    ((dpl_list*) ((dpl_list*)pos)->next) ->prev = node;
    ((dpl_list*)pos)->next = node;
}


static void dpl_list_insert_before(dpl_list_type *pos, dpl_list_type *node)
{
    dpl_list_insert_after(((dpl_list*)pos)->prev, node);
}


static void dpl_list_insert_nodes_after(dpl_list_type *pos, dpl_list_type *lst)
{
    dpl_list *lst_last = (dpl_list *) ((dpl_list*)lst)->prev;
    dpl_list *pos_next = (dpl_list *) ((dpl_list*)pos)->next;

    dpl_link_node(pos, lst);
    dpl_link_node(lst_last, pos_next);
}

static void dpl_list_insert_nodes_before(dpl_list_type *pos, dpl_list_type *lst)
{
    dpl_list_insert_nodes_after(((dpl_list*)pos)->prev, lst);
}

static void dpl_list_merge_last(dpl_list_type *lst1, dpl_list_type *lst2)
{
    if (!dpl_list_empty(lst2)) {
	dpl_link_node(((dpl_list*)lst1)->prev, ((dpl_list*)lst2)->next);
	dpl_link_node(((dpl_list*)lst2)->prev, lst1);
	dpl_list_init(lst2);
    }
}

static void dpl_list_merge_first(dpl_list_type *lst1, dpl_list_type *lst2)
{
    if (!dpl_list_empty(lst2)) {
	dpl_link_node(((dpl_list*)lst2)->prev, ((dpl_list*)lst1)->next);
	dpl_link_node(((dpl_list*)lst1), ((dpl_list*)lst2)->next);
	dpl_list_init(lst2);
    }
}

static void dpl_list_erase(dpl_list_type *node)
{
    dpl_link_node( ((dpl_list*)node)->prev, ((dpl_list*)node)->next);

    /* It'll be safer to init the next/prev fields to itself, to
     * prevent multiple erase() from corrupting the list. See
     * ticket #520 for one sample bug.
     */
    dpl_list_init(node);
}


static dpl_list_type* dpl_list_find_node(dpl_list_type *list, dpl_list_type *node)
{
    dpl_list *p = (dpl_list *) ((dpl_list*)list)->next;
    while (p != list && p != node)
	p = (dpl_list *) p->next;

    return p==node ? p : NULL;
}


static dpl_list_type* dpl_list_search(dpl_list_type *list, void *value,
	       		int (*comp)(void *value, const dpl_list_type *node))
{
    dpl_list *p = (dpl_list *) ((dpl_list*)list)->next;
    while (p != list && (*comp)(value, p) != 0)
	p = (dpl_list *) p->next;

    return p==list ? NULL : p;
}


static dpl_size_t dpl_list_size(const dpl_list_type *list)
{
    const dpl_list *node = (const dpl_list*) ((const dpl_list*)list)->next;
    dpl_size_t count = 0;

    while (node != list) {
	++count;
	node = (dpl_list*)node->next;
    }

    return count;
}



#endif	/* __DPL_LIST_H__ */

